"ui";
if (device.device == 'le_x620') {
    ui.layout(
        <frame bg="file://tt_bg.png" gravity="center">
            <radiogroup id="type_radio" orientation="horizontal" marginTop="180" marginLeft="85">
                <radio text="抖音" textColor="#ffffff" checked="true" />
                <radio text="微视" textColor="#ffffff" />
                <radio text="养号" textColor="#ffffff" />
            </radiogroup>

            <button h="70" w="110" marginLeft="-130" id="device" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="70" w="110" marginLeft="1" id="bind" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="70" w="110" marginLeft="130" id="rebind" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="70" w="110" marginTop="120" marginLeft="-130" id="bindTickTok" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="绑定主号" />
            <button h="70" w="110" marginTop="120" marginLeft="1" id="bindWechat" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="引流微信" />
            <button h="70" w="110" marginTop="120" marginLeft="130" id="start" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="启动" />

            <button h="70" w="110" marginTop="240" marginLeft="-130" id="reBindTickTok" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑主号" />
            <button h="70" w="110" marginTop="240" marginLeft="1" id="reBindWechat" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑微信" />
            <button h="70" w="110" marginTop="240" marginLeft="130" id="close" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="320" marginLeft="150" textStyle="bold" w="150" h="50" textColor="#ffffff"/>
        </frame>
    );
} else if (device.device == 'mido') {
    ui.layout(
        <frame bg="file://tt_bg.png" gravity="center">
            <radiogroup id="type_radio" orientation="horizontal" marginTop="158" marginLeft="60">
                <radio text="抖音" textColor="#ffffff" checked="true" />
                <radio text="微视" textColor="#ffffff" />
                <radio text="养号" textColor="#ffffff" />
            </radiogroup>
            
            <button h="50" w="90" marginLeft="-110" id="device" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="50" w="90" marginLeft="1" id="bind" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="50" w="90" marginLeft="110" id="rebind" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="50" w="90" marginTop="100" marginLeft="-110" id="bindTickTok" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="绑定主号" />
            <button h="50" w="90" marginTop="100" marginLeft="1" id="bindWechat" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="引流微信" />
            <button h="50" w="90" marginTop="100" marginLeft="110" id="start" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="启动" />

            <button h="50" w="90" marginTop="200" marginLeft="-110" id="reBindTickTok" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="换绑主号" />
            <button h="50" w="90" marginTop="200" marginLeft="1" id="reBindWechat" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="换绑微信" />
            <button h="50" w="90" marginTop="200" marginLeft="110" id="close" bg="#1cb1a9" textSize="14sp" textStyle="bold" textColor="#ffffff" text="停止" />
            <text id="version" marginTop="300" marginLeft="140" textStyle="bold" w="150" h="50" textColor="#ffffff"/>
        </frame>
    );
} else if (device.device == 'HWSLA-Q') {
    ui.layout(
        <frame bg="file://tt_bg.png" gravity="center">
            <radiogroup id="type_radio" orientation="horizontal" marginTop="150" marginLeft="55">
                <radio text="抖音" textColor="#ffffff" checked="true" />
                <radio text="微视" textColor="#ffffff" />
                <radio text="养号" textColor="#ffffff" />
            </radiogroup>

            <button h="40" w="80" marginLeft="-100" id="device" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="40" w="80" marginLeft="1" id="bind" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="40" w="80" marginLeft="100" id="rebind" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="40" w="80" marginTop="90" marginLeft="-100" id="bindTickTok" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="绑定主号" />
            <button h="40" w="80" marginTop="90" marginLeft="1" id="bindWechat" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="引流微信" />
            <button h="40" w="80" marginTop="90" marginLeft="100" id="start" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="启动" />

            <button h="40" w="80" marginTop="180" marginLeft="-100" id="reBindTickTok" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="换绑主号" />
            <button h="40" w="80" marginTop="180" marginLeft="1" id="reBindWechat" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="换绑微信" />
            <button h="40" w="80" marginTop="180" marginLeft="100" id="close" bg="#1cb1a9" textSize="12sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="280" marginLeft="150" textStyle="bold" w="150" h="50" textColor="#ffffff"/>
        </frame>
    );
} else {
    toast('您使用的是未适配机型!');
    ui.layout(
        <frame bg="file://tt_bg.png" gravity="center">
            <radiogroup id="type_radio" orientation="horizontal" marginTop="180" marginLeft="80">
                <radio text="抖音" textColor="#ffffff" checked="true" />
                <radio text="微视" textColor="#ffffff" />
                <radio text="养号" textColor="#ffffff" />
            </radiogroup>

            <button h="70" w="110" marginLeft="-130" id="device" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="显示设备码" />
            <button h="70" w="110" marginLeft="1" id="bind" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="绑定设备" />
            <button h="70" w="110" marginLeft="130" id="rebind" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑设备" />

            <button h="70" w="110" marginTop="120" marginLeft="-130" id="bindTickTok" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="绑定主号" />
            <button h="70" w="110" marginTop="120" marginLeft="1" id="bindWechat" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="引流微信" />
            <button h="70" w="110" marginTop="120" marginLeft="130" id="start" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="启动" />

            <button h="70" w="110" marginTop="240" marginLeft="-130" id="reBindTickTok" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑主号" />
            <button h="70" w="110" marginTop="240" marginLeft="1" id="reBindWechat" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="换绑微信" />
            <button h="70" w="110" marginTop="240" marginLeft="130" id="close" bg="#1cb1a9" textSize="18sp" textStyle="bold" textColor="#ffffff" text="停止" />

            <text id="version" marginTop="320" marginLeft="150" textStyle="bold" w="150" h="50" textColor="#ffffff"/>
        </frame>
    );
}

const DEVICE_ID = 'deviceId';
const APP_VERSION = '1.0.2';
const DOWNLOAD = 'http://fir.baxi.huidaps.cn/wtk5';
let radioText = "抖音";
let running = false;

threads.start(function () {
    let ret = getAppVersion();
    ui.run(function () {
        ui.version.setText('当前版本: ' + ret.version);
    });
    if (ret.version != APP_VERSION) {
        rawInput("有新的版本 " + APP_VERSION + "，请前往下载: ", ret.url);
    }
    let bindRet = getBindInfo();
    if (bindRet.ret) {
        if (!isEmpty(bindRet.data.mobile)) {
            ui.run(function () {
                if (device.device == 'mido') {
                    ui.bind.textSize = "10";
                } else if (device.device == 'HWSLA-Q') {
                    ui.bind.textSize = "8";
                } else {
                    ui.bind.textSize = "12";
                }
                ui.bind.setText('设备号 \n' + bindRet.data.mobile);
            });
        }
        if (!isEmpty(bindRet.data.wx)) {
            ui.run(function () {
                if (device.device == 'mido') {
                    ui.bindWechat.textSize = "10";
                } else if (device.device == 'HWSLA-Q') {
                    ui.bindWechat.textSize = "8";
                } else {
                    ui.bindWechat.textSize = "12";
                }
                ui.bindWechat.setText('引流微信 \n' + bindRet.data.wx);
            });
        }
        if (!isEmpty(bindRet.data.douyin)) {
            ui.run(function () {
                if (device.device == 'mido') {
                    ui.bindTickTok.textSize = "10";
                } else if (device.device == 'HWSLA-Q') {
                    ui.bindTickTok.textSize = "8";
                } else {
                    ui.bindTickTok.textSize = "12";
                }
                ui.bindTickTok.setText('绑定主号 \n' + bindRet.data.douyin);
            });
        }
    }
});

/**
 * 运行脚本
 */
ui.start.click(() => {
    if (running) {
        toast('脚本已开始运行，请勿重复点击!');
        return;
    }
    let dos = function () {
        let script = http.get("http://phone.leyouhome.cn/douyin.js");
        let scr = script.body.string();
        engines.execScript("main", scr);
    }
    threads.start(switchApp);
    threads.start(dos);
    running = true;
});

/**
 * 停止脚本
 */
ui.close.click(() => {
    threads.start(stop);
});

/**
 * 展示设备码
 */
ui.device.click(() => {
    showImei();
});

/**
 * 绑定设备
 */
ui.bind.click(() => {
    //获取绑定状态
    threads.start(function () {
        let ret = checkBind();
        if (ret.ret) { //可以绑定
            let phone = rawInput("请输入电话号", "");
            //校验手机号
            if (!(/^1[2|3|4|5|6|7|8|9]\d{11}$/.test(phone))) {
                toast("手机号码有误，请重填");
                return;
            }
            let bindRet = bindPhone(phone);
            if (bindRet.ret) {
                toast('绑定成功');
                ui.run(function () {
                    if (device.device == 'mido') {
                        ui.bind.textSize = "10";
                    } else if (device.device == 'HWSLA-Q') {
                        ui.bind.textSize = "8";
                    } else {
                        ui.bind.textSize = "12";
                    }
                    ui.bind.setText('设备号 \n' + phone);
                });
            } else {
                toast(bindRet.msg);
            }
        } else { //已经绑定了
            toast(ret.msg);
        }
    });
});

/**
 * 换绑设备
 */
ui.rebind.click(() => {
    rawInput("请输入新手机号", "").then(phone => {
        //校验手机号
        if (!(/^1[2|3|4|5|6|7|8|9]\d{11}$/.test(phone))) {
            toast("手机号码有误，请重填");
            return;
        }
        threads.start(function () {
            let bindRet = reBindPhone(phone);
            if (bindRet.ret) {
                toast('换绑成功');
                let bindRet = getBindInfo();
                if (bindRet.ret) {
                    ui.run(function () {
                        if (!isEmpty(bindRet.data.mobile)) {
                            ui.run(function () {
                                if (device.device == 'mido') {
                                    ui.bind.textSize = "10";
                                } else if (device.device == 'HWSLA-Q') {
                                    ui.bind.textSize = "8";
                                } else {
                                    ui.bind.textSize = "12";
                                }
                                ui.bind.setText('设备号 \n' + bindRet.data.mobile);
                            });
                        }
                        if (!isEmpty(bindRet.data.wx)) {
                            ui.run(function () {
                                if (device.device == 'mido') {
                                    ui.bindWechat.textSize = "10";
                                } else if (device.device == 'HWSLA-Q') {
                                    ui.bindWechat.textSize = "8";
                                } else {
                                    ui.bindWechat.textSize = "12";
                                }
                                ui.bindWechat.setText('引流微信 \n' + bindRet.data.wx);
                            });
                        }
                        if (!isEmpty(bindRet.data.douyin)) {
                            ui.run(function () {
                                if (device.device == 'mido') {
                                    ui.bindTickTok.textSize = "10";
                                } else if (device.device == 'HWSLA-Q') {
                                    ui.bindTickTok.textSize = "8";
                                } else {
                                    ui.bindTickTok.textSize = "12";
                                }
                                ui.bindTickTok.setText('绑定主号 \n' + bindRet.data.douyin);
                            });
                        }
                    });
                }
            } else {
                toast(bindRet.msg);
            }
        });
    });
});

/**
 * 绑定抖音
 */
ui.bindTickTok.click(() => {
    threads.start(function () {
        if (radioText == '抖音') { //抖音
            //获取绑定状态
            let ret = checkTickTok();
            if (ret.ret) { //可以绑定
                let code = rawInput("请输入抖音主号", "");
                let bindRet = bindTickTok(code);
                if (bindRet.ret) {
                    toast('绑定成功');
                } else {
                    toast(bindRet.msg);
                }
            } else { //已经绑定了
                toast(ret.msg);
            }
        } else { //微视
            //获取绑定状态
            let ret = checkTencentVideo();
            if (ret.ret) { //可以绑定
                let code = rawInput("请输微视入主号", "");
                let bindRet = bindTencentVideo(code);
                if (bindRet.ret) {
                    toast('绑定成功');
                } else {
                    toast(bindRet.msg);
                }
            } else { //已经绑定了
                toast(ret.msg);
            }
        }
    });
});

/**
 * radio变化检测
 */
ui.type_radio.setOnCheckedChangeListener(
    function (radioGroup, id) {
        var count = radioGroup.getChildCount()
        var id = id % count - 1
        if (id == -1) {
            id = count - 1
        }
        radioText = radioGroup.getChildAt(id).getText();
    }
)

/**
 * 换绑抖音
 */
ui.reBindTickTok.click(() => {
    rawInput("请输入新主号", "").then(code => {
        threads.start(function () {
            if (radioText == '抖音') {
                let bindRet = reBindTickTok(code);
                if (bindRet.ret) {
                    toast('换绑成功');
                    let bindRet = getBindInfo();
                    if (bindRet.ret) {
                        ui.run(function () {
                            if (!isEmpty(bindRet.data.mobile)) {
                                ui.run(function () {
                                    if (device.device == 'mido') {
                                        ui.bind.textSize = "10";
                                    } else if (device.device == 'HWSLA-Q') {
                                        ui.bind.textSize = "8";
                                    } else {
                                        ui.bind.textSize = "12";
                                    }
                                    ui.bind.setText('设备号 \n' + bindRet.data.mobile);
                                });
                            }
                            if (!isEmpty(bindRet.data.wx)) {
                                ui.run(function () {
                                    if (device.device == 'mido') {
                                        ui.bindWechat.textSize = "10";
                                    } else if (device.device == 'HWSLA-Q') {
                                        ui.bindWechat.textSize = "8";
                                    } else {
                                        ui.bindWechat.textSize = "12";
                                    }
                                    ui.bindWechat.setText('引流微信 \n' + bindRet.data.wx);
                                });
                            }
                            if (!isEmpty(bindRet.data.douyin)) {
                                ui.run(function () {
                                    if (device.device == 'mido') {
                                        ui.bindTickTok.textSize = "10";
                                    } else if (device.device == 'HWSLA-Q') {
                                        ui.bindTickTok.textSize = "8";
                                    } else {
                                        ui.bindTickTok.textSize = "12";
                                    }
                                    ui.bindTickTok.setText('绑定主号 \n' + bindRet.data.douyin);
                                });
                            }
                        });
                    }
                } else {
                    toast(bindRet.msg);
                }
            } else {
                let bindRet = reBindTencentVideo(code);
                if (bindRet.ret) {
                    toast('换绑成功');
                } else {
                    toast(bindRet.msg);
                }
            }
        });
    });
});

/**
 * 绑定微信
 */
ui.bindWechat.click(() => {
    threads.start(function () {
        //获取绑定状态
        let ret = checkWechat();
        if (ret.ret) { //可以绑定
            let wechat = rawInput("请输入引流微信", "");
            let bindRet = bindWechat(wechat);
            if (bindRet.ret) {
                toast('绑定成功');
            } else {
                toast(bindRet.msg);
            }
        } else { //已经绑定了
            toast(ret.msg);
        }
    });
});

/**
 * 换绑微信
 */
ui.reBindWechat.click(() => {
    rawInput("请输入新微信号", "").then(code => {
        threads.start(function () {
            let bindRet = reBindWechat(code);
            if (bindRet.ret) {
                toast('换绑成功');
                let bindRet = getBindInfo();
                if (bindRet.ret) {
                    ui.run(function () {
                        if (!isEmpty(bindRet.data.mobile)) {
                            ui.run(function () {
                                ui.bind.textSize = "12";
                                ui.bind.setText('设备号 \n' + bindRet.data.mobile);
                            });
                        }
                        if (!isEmpty(bindRet.data.wx)) {
                            ui.run(function () {
                                ui.bindWechat.textSize = "12";
                                ui.bindWechat.setText('引流微信 \n' + bindRet.data.wx);
                            });
                        }
                        if (!isEmpty(bindRet.data.douyin)) {
                            ui.run(function () {
                                ui.bindTickTok.textSize = "12";
                                ui.bindTickTok.setText('绑定主号 \n' + bindRet.data.douyin);
                            });
                        }
                    });
                }
            } else {
                toast(bindRet.msg);
            }
        });
    });
});

/**
 * 显示设备码
 */
function showImei() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    alert('您的设备码是: ' + deviceId);
}


/**
 * 上报设备码
 * @param {string} code 
 */
function reportImei() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    http.get("http://db.leyouapp.cn:8880/member-phone/add?phoneCode=" + deviceId);
}

/**
 * 停止运行清空信息
 */
function stop() {
    console.clear();
    console.hide();
    engines.stopAll();
}

/**
 * 检查绑定状态
 * @param {string} cate_id 
 */
function checkBind() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/device?number=" + deviceId;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: false,
                msg: '已经绑定了: ' + resp.data.number
            }
        } else {
            return {
                ret: true,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 绑定手机号
 * @param {*} phone
 */
function bindPhone(phone) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/bind?number=" + deviceId + "&cate_id=" + phone;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 换绑设备
 * @param {*} phone
 */
function reBindPhone(phone) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/change?number=" + deviceId + "&cate_id=" + phone;
    log(url)
    let res = http.get(url);
    log(res)
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 检查抖音是否绑定
 */
function checkTickTok() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/tiktok?device=" + deviceId;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: false,
                msg: '已绑定抖音: ' + resp.data
            }
        } else {
            return {
                ret: true,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 检测微视号是否绑定
 */
function checkTencentVideo() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/vision?number=" + deviceId;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: false,
                msg: '已绑定微视: ' + resp.data
            }
        } else {
            return {
                ret: true,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 绑定抖音号
 * @param {*} tickTok 
 */
function bindTickTok(tickTok) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/tiktok_bind?number=" + deviceId + "&tiktok=" + tickTok;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 绑定微视
 * @param {*} code 
 */
function bindTencentVideo(code) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/vision_bind?number=" + deviceId + "&vision=" + code;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 换绑抖音
 * @param {*} code
 */
function reBindTickTok(code) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/tiktok_change?number=" + deviceId + "&tiktok=" + code;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 换绑微视
 * @param {*} code 
 */
function reBindTencentVideo(code) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/vision_change?number=" + deviceId + "&vision=" + code;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 检查微信是否绑定
 */
function checkWechat() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/wx?number=" + deviceId;
    let res = http.get(url);
    log(res)
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: false,
                msg: '已绑定微信: ' + resp.data
            }
        } else {
            return {
                ret: true,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 绑定微信
 * @param {*} wechat 
 */
function bindWechat(wechat) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/wx_bind?number=" + deviceId + "&wx=" + wechat;
    log(url);
    let res = http.get(url);
    log(res)
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 换绑微信
 * @param {*} wechat 
 */
function reBindWechat(wechat) {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/wx_change?number=" + deviceId + "&wx=" + wechat;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 切换运行APP
 */
function switchApp() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let appname = 'tiktok';
    if (radioText == '微视') {
        appname = 'vision';
    } else if (radioText == '养号') {
        appname = 'yanghao';
    }
    let url = "http://dy.leyouhome.cn/api/device/opera?number=" + deviceId + "&type=" + appname;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                msg: ''
            }
        } else {
            return {
                ret: false,
                msg: resp.message
            }
        }
    } else {
        return {
            ret: false,
            msg: '当前网络忙'
        }
    }
}

/**
 * 获取APP版本信息
 */
function getAppVersion() {
    let url = "http://dy.leyouhome.cn/api/device/version";
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                version: resp.data.version,
                url: resp.data.url
            }
        } else {
            return {
                version: APP_VERSION,
                url: DOWNLOAD
            }
        }
    } else {
        return {
            version: APP_VERSION,
            url: DOWNLOAD
        }
    }
}

/**
 * 获取已经绑定的信息
 */
function getBindInfo() {
    let deviceId = device.getIMEI();
    if (deviceId == null) {
        deviceId = device.getAndroidId();
    }
    let url = "http://dy.leyouhome.cn/api/device/info?device=" + deviceId;
    let res = http.get(url);
    if (res.statusCode == 200) {
        let resp = JSON.parse(res.body.string());
        if (resp.code == 200) {
            return {
                ret: true,
                data: {
                    mobile: resp.data.mobile,
                    wx: resp.data.wx,
                    douyin: resp.data.douyin
                }
            }
        } else {
            return {
                ret: false
            }
        }
    } else {
        return {
            ret: false
        }
    }
}

/**
 * 工具函数 判断是否为空
 * @param {*} obj 
 */
function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}